package com.dsg.controller.bank;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountTrade;
import com.dsg.entity.bank.DsgBankDetail;
import com.dsg.service.bank.IDsgBankDetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 銀行往來明細資料 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@RestController
@RequestMapping("/bank/dsgBankDetail")
public class DsgBankDetailController extends BaseController {

    @Autowired
    private IDsgBankDetailService dsgBankDetailService;

    /**
     * 上載銀行過數
     *
     * @param dsgAccountTrade
     * @return
     */
    @PutMapping(value = "/bankAccount")
    @ApiOperation("上載銀行過數")
    public AjaxResult bankAccount(@RequestBody DsgAccountTrade dsgAccountTrade) {
        dsgBankDetailService.bankAccount(dsgAccountTrade);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }

    /**
     * 查询列表（分页）
     *
     * @param dsgBankDetail
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgBankDetail dsgBankDetail) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        if (StringUtil.isEmpty(sort)) {
            orderItemList.add(new OrderItem().setColumn("CREATE_TIME").setAsc(false));
            page.setOrders(orderItemList);
        }
        dsgBankDetail.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgBankDetailService.selectByList(page, dsgBankDetail);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 修改上載過數結果狀態
     *
     * @param idList 主键
     * @param status 3：成功 4：失敗
     * @return
     */
    @PutMapping(value = "/updateIsAccept")
    @ApiOperation("修改上載過數結果狀態")
    public AjaxResult updateStatus(@ApiParam(value = "id以,号分隔", name = "idList", required = true) @RequestParam("idList") String idList,
                                   @ApiParam(value = "狀態 3：成功 4：失敗", name = "status", required = true) @RequestParam("status") Integer status) {
        dsgBankDetailService.updateStatus(idList, status);
        return result(MessageConstants.SSO_STATUS_SUCCESS);
    }
}
